#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const process = require('process');
const paths = require('../lib/paths.js');
require('shelljs');

/**
 * @param gitUrl 框架git地址
 * @param destPath 临时存放路径
 */

module.exports = function downLoadGit (gitUrl, destPath, branch) {
    return new Promise ((resolve, reject) => {
        if (test('-L', 'git')) {
            throw '请先安装git'.red;
        }
        if (test('-d', destPath)) { // 如果存在目录，先删除目录
            rm('-rf', destPath);
        }
        // console.log(branch);
        let cmdStr = branch ? `git clone -b ${branch} ${gitUrl}` : `git clone ${gitUrl}`
        echo (cmdStr.blue);
        // return false;
        exec(cmdStr, (code) => {
            if (0 === parseInt(code)) {
                resolve();
            } else {
                reject(code);
            }
        })
    });
}
